package model;

import entities.Department;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.AbstractTableModel;

public class EditDepartmentTableModel extends AbstractTableModel {

    ArrayList<Department> list;

    public EditDepartmentTableModel() {
        list = new ArrayList<Department>();
    }

    public EditDepartmentTableModel(ArrayList<Department> list) {
        this.list = list;
    }

    @Override
    public int getRowCount() {
        return list.size();
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Department department = list.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return department.getName();
            case 1:
                return department.getLocation();
            case 2:
                return department.getPhone();
            case 3:
                return department.getDescription();
            case 4:
                return department.getModifiedDate();
            default:
                return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return String.class;
            case 1:
                return String.class;
            case 2:
                return String.class;
            case 3:
                return String.class;
            case 4:
                return Date.class;
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Department";
            case 1:
                return "Location";
            case 2:
                return "Phone Contact";
            case 3:
                return "Description";
            case 4:
                return "ModifiedDate";
            default:
                return null;
        }
    }

    @Override
    public void fireTableDataChanged() {
        super.fireTableDataChanged();
    }

    public boolean addAll(ArrayList<Department> list) {
        boolean returnVal = this.list.addAll(list);
        if (returnVal) {
            fireTableDataChanged();
        }
        return returnVal;
    }

    public boolean addElement(Department department) {
        boolean returnVal = list.add(department);
        if (returnVal) {
            fireTableDataChanged();
        }
        return returnVal;
    }

    public Department getAt(int index) {
        return list.get(index);
    }

    public void removeAt(int index) {
        list.remove(index);
        fireTableDataChanged();
    }
    
    public void remove(Department dep) {
        list.remove(dep);
        fireTableDataChanged();
    }

    public void update(int index, Department department) {
        list.set(index, department);
        fireTableDataChanged();
    }
    
    public void update(Department dep) {
        list.set(list.indexOf(dep), dep);
        fireTableDataChanged();
    }

    public void clear() {
        list.clear();
    }
}
